package com.ly.algorithm.leetcode.hashtable;

import java.util.*;

/**
 * @Classname Problem49
 * @Description
 *
 * 示例:
 *
 * 输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
 * 输出:
 * [
 *   ["ate","eat","tea"],
 *   ["nat","tan"],
 *   ["bat"]
 * ]
 *
 * @Date 2020/12/20 13:53
 * @Author 冷心影翼
 */
public class Problem49 {

	public static void main(String[] args) {
		Solution49 solution49 = new Solution49();
		List<List<String>> lists = solution49.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
		System.out.println(lists);
	}
}


class Solution49 {

	public List<List<String>> groupAnagrams(String[] strs) {
		Map<String,List<String>> hashMap = new HashMap<>();
		for(String str : strs) {
			char[] chars = str.toCharArray();
			Arrays.sort(chars);
			String key = new String(chars);
			if(hashMap.containsKey(key)) {
				List<String> list = hashMap.get(key);
				list.add(str);
				hashMap.put(key,list);
			}else {
				List<String> list = new ArrayList<>();
				list.add(str);
				hashMap.put(key,list);
			}
		}

 		return new ArrayList<>(hashMap.values());
	}
}